package main

import (
	"fmt"
)

type Josephu struct {
	num  int
	next *Josephu
}

func list(head *Josephu) {
	fmt.Println()
	temp := head
	if head.next == nil {
		fmt.Println("空空如也")
		return
	}
	for {
		fmt.Printf("%v,", temp.num)
		if temp.next == head {
			break
		}
		temp = temp.next
	}
	fmt.Println()
}
func delete(head *Josephu, startNo int, countNum int) {
	first := head
	tail := head
	if first.next == nil {
		fmt.Println("环形连表为空")
		return
	}
	for {
		if tail.next == first {
			break
		}
		tail = tail.next
	}
	for i := 1; i <= startNo-1; i++ {
		first = first.next
		tail = tail.next
	}
	for {
		for i := 1; i <= countNum-1; i++ {
			first = first.next
			tail = tail.next
		}
		fmt.Printf("出局顺序为:%d\n", first.num)
		first = first.next
		tail.next = first
		if first == tail {
			break
		}
	}
}
func push(head *Josephu, newNode *Josephu) {
	temp := head
	if temp.next == nil {
		head.next = newNode
		newNode.next = head
		return
	}
	flag := false
	for {
		if temp.next == head {
			break
		}
		if temp.next.num >= newNode.num {
			newNode.next = temp.next
			temp.next = newNode
			flag = true
			break
		}
		temp = temp.next
	}

	if !flag {
		if temp.num >= newNode.num {
			newNode.next = temp.next
			temp.next = newNode
		} else {
			newNode.next = head
			temp.next = newNode
		}
	}
}
